var $$ = jQuery.noConflict();
(function ($, doc) {
    $.init();
    $.ready(function () {
        //发送验证码
        $$('#sendCode').one('touchend',function () {
            phoneSms($$('#sendCode'))
            this.innerHTML = '60秒后重发'
            countDown($$(this))
        })
        //发送短信验证码
        function phoneSms(_this) {
            $$.post('/checkPhone', {'phone': $$('#phone').val(),'type':'wechat'}, function (data) {
                if (data.code == 10002) {
                    $$.post('/sendSms', {'phone': $$('#phone').val(),'type':'wechat'}, function (data) {
                        if (data.code != 10000) {
                            mui.alert(data.message);
                        }
                    })
                } else {
                    mui.alert(data.message);
                }
            })
        }

//60秒倒计时s
        function countDown(dom) {
            var timer, k = 60,
                i = 0
            timer = setInterval(function () {
                i++;
                dom.html((k - i) + '秒后重发')
                if ((k - i) == 0) {
                    dom.html('获取验证码')
                    dom.one('touchend', function () {
                        phoneSms(dom)
                        this.innerHTML = '60秒后重发'
                        countDown($$(this))
                    })
                    clearInterval(timer)
                }
            }, 1000)
        }

        //form表单
        $$('#send').on('touchend', function () {
            var i = 0;
            mui("#formData input").each(function () {
                if (!this.value || this.value.trim() == "") {
                    var label = this.previousElementSibling;
                    mui.alert(label.innerText + "不允许为空");
                    return false;
                } else {
                    i = i + 1
                }
            });
            if (i == 3) {
                $$('#send').prop('disabled','true')
                if (/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test($$('#phone').val())) {
                    if (/^\d{6}$/.test($$('#yanzheng').val())) {
                        if (/((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}$/.test($$('#password').val())) {
                            $.post('/register', {
                                'phone': $$('#phone').val(),
                                'password': $$('#password').val(),
                                'codeParam': $$('#yanzheng').val(),
                                'type':'wechat'
                            }, function (data) {
                                $$('#send').prop('disabled','false')
                                if (data.code == 10001) {
                                    $.post('/login',{'phone':$$('#phone').val(),'password':$$('#password').val(),'type':'wechat'},function (data) {
                                        if(data.code==10013){
                                            openPage('/wechat/toMain')
                                        }else if(data.code==20007){
                                            mui.alert('用户名不存在!');
                                        }else if(data.code==10012){
                                            mui.alert('请先绑定微信');
                                            window.location.href='/wechat/toBind'
                                        }else if(data.code==20008){
                                            mui.alert('用户名或密码错误!');
                                        }
                                    })
                                } else if (data.code == 20009) {
                                    mui.alert('您的手机号已注册!');
                                } else if (data.code == 20006) {
                                    mui.alert('请输入正确的短信验证码');
                                }
                            })
                        } else {
                            mui.alert('请输入8-16位且数字、字母、符号至少包含两种');
                        }
                    } else {
                        mui.alert('请输入6位验证码!');
                    }
                } else {
                    mui.alert('请输入正确手机号!');
                }
            }
        })
    });
})(mui, document);
